package cn.edu.hrbcu.es.invigilatemanagerment.service;

import cn.edu.hrbcu.es.invigilatemanagerment.configuration.DBUserDetailsManager;
import cn.edu.hrbcu.es.invigilatemanagerment.dao.TeacherCourseMapper;
import cn.edu.hrbcu.es.invigilatemanagerment.dao.TeacherMapper;
import cn.edu.hrbcu.es.invigilatemanagerment.pojo.RoleExample;
import cn.edu.hrbcu.es.invigilatemanagerment.pojo.Teacher;
import cn.edu.hrbcu.es.invigilatemanagerment.pojo.TeacherExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.apache.poi.ss.formula.functions.T;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTDateAx;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

@Service
public class TeacherServiceImpl implements ITeacherService{
    @Autowired
    private TeacherMapper teacherMapper;

    @Resource
    private DBUserDetailsManager dbUserDetailsManager;

    @Override
    public List<Teacher> queryAllTeacher() {
        TeacherExample example = new TeacherExample();
        TeacherExample.Criteria criteria = example.createCriteria();
        criteria.andIdGreaterThan(0);
        List<Teacher> teachers = teacherMapper.selectByExample(example);
        return teachers;
    }

    @Override
    public Teacher queryTeacherById(Integer id) {
        return teacherMapper.selectByPrimaryKey(id);
    }

    /**
     * 这个方法只是添加一个新的账号，对应的账号信息在添加成功，再完善
     * @param teacher
     * @return
     */
    @Override
    public Integer insertTeacher(Teacher teacher) {
       List<Teacher> teachers = queryTeacherByUsername(teacher.getUsername());
        if(teachers != null && teachers.size() > 0){
            return -1;
        }

        // 金雪松修改
        UserDetails userDetails = User
                .withDefaultPasswordEncoder()
                .username(teacher.getUsername())
                .password(teacher.getPassword()).build();
        dbUserDetailsManager.createUser(userDetails);

        //通过username获取已插入的teacher，更新其余信息
        TeacherExample teacherExample = new TeacherExample();
        TeacherExample.Criteria criteria = teacherExample.createCriteria();
        criteria.andUsernameEqualTo(teacher.getUsername());
        Teacher newteacher = teacherMapper.selectByExample(teacherExample).get(0);
        newteacher.setEmployeenum(teacher.getEmployeenum());
        newteacher.setName(teacher.getName());
        newteacher.setMobile(teacher.getMobile());
        newteacher.setIsPresent(teacher.getIsPresent());
        newteacher.setTeachernum(teacher.getTeachernum());
        newteacher.setSpecialityId(teacher.getSpecialityId());
        teacherMapper.updateByPrimaryKey(newteacher);
        return 1;
    }

    @Override
    public Integer deleteTeacherById(Integer id) {
        return teacherMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Integer updateTeacherById(Teacher teacher) {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encodePassword = bCryptPasswordEncoder.encode(teacher.getPassword());
        teacher.setPassword("{bcrypt}" + encodePassword);
        return teacherMapper.updateByPrimaryKey(teacher);
    }

    @Override
    public List<Teacher> queryTeacherByUsername(String username) {
        TeacherExample example = new TeacherExample();
        TeacherExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(username);
        return teacherMapper.selectByExample(example);
    }

    @Override
    public List<Teacher> queryTeacherByName(String name) {
        TeacherExample example = new TeacherExample();
        TeacherExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(name);
        return teacherMapper.selectByExample(example);
    }

    @Override
    public List<Teacher> queryTeacherByUserName(String username) {
        TeacherExample example = new TeacherExample();
        TeacherExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(username);
        return teacherMapper.selectByExample(example);
    }

    @Override
    public int queryTeacherCount() {
        TeacherExample teacherExample = new TeacherExample();
        TeacherExample.Criteria criteria = teacherExample.createCriteria();
        criteria.getAllCriteria();
        return (int) teacherMapper.countByExample(teacherExample);
    }

    @Override
    public List<Teacher> queryTeacherByPage(int pageNow, int pageSize) {
        PageHelper.startPage(pageNow,pageSize);
        List<Teacher> teachers = this.queryAllTeacher();
        PageInfo<Teacher> teacherPageInfo = new PageInfo<>(teachers);
        return teacherPageInfo.getList();
    }
}
